﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmCustomerMgr : Form
    {
        public List<ComboItem> collection = new List<ComboItem>();
        public FrmCustomerMgr()
        {
            InitializeComponent();
        }
        private void LoadData()
        {
            this.cJ_CUSTOMER_INFOTableAdapter.Fill(this.cjwyglDataSet_customerinfo.CJ_CUSTOMER_INFO);

        }
        private void FrmCustomerMgr_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“cjwyglDataSet_customerinfo.CJ_CUSTOMER_INFO”中。您可以根据需要移动或删除它。
            LoadData();
            
            collection.Add(new ComboItem { Text = "所有列", Value = "ALL" });
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                collection.Add(new ComboItem
                {
                    Text = dataGridView1.Columns[i].HeaderText,
                    Value = dataGridView1.Columns[i].DataPropertyName
                });
            }
            comboBox_search.DisplayMember = "Text";
            comboBox_search.ValueMember = "Value";
            comboBox_search.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox_search.DataSource = collection;
            comboBox_search.SelectedValue = "ALL";
            
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            FrmCustomerUpdate frmUpdate = new FrmCustomerUpdate();
            frmUpdate.ShowDialog();
        }

        private void button_update_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                FrmCustomerUpdate frmUpdate = new FrmCustomerUpdate(id);
                frmUpdate.ShowDialog();

            }
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗?", "删除确认", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
                return;
            int id;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);

            }
            using (DataBase db = new DataBase())
            {
                if (db.ExecuteNonQuery("delete from [CJ_CUSTOMER_INFO] WHERE ID=" + id) == 1)
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void button_search_Click(object sender, EventArgs e)
        {
            string sql = "";
            string key=textBox_search.Text.Trim();
            if (key == "")
            {
                MessageBox.Show("请输入查询关键词");
                return;
            }
            if (comboBox_search.SelectedValue.ToString() == "ALL")
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from CJ_CUSTOMER_INFO where 1=2 ");
                for (int i = 1; i < collection.Count;i++ )
                {
                    sb.Append("or " + collection[i].Value + " like '%" + key + "%' ");
                }
                sql = sb.ToString();
            }
            else
            {
                sql = "select * from CJ_CUSTOMER_INFO where " +
                    comboBox_search.SelectedValue + " like '%" + key + "%' ";
            }
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable(sql);
                this.cJCUSTOMERINFOBindingSource.DataSource = dt;
                LoadData();
            }
        }


    }
    public class ComboItem
    {
        public string Text{get;set;}
        public string Value{get;set;}
    }
}
